package org.glut.wechat.moments.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.glut.wechat.moments.domain.Moments;
import org.glut.wechat.moments.dto.MomentPublishDTO;
import org.glut.wechat.moments.dto.MomentUpdateDTO;
import org.glut.wechat.moments.service.MomentsService;
import org.glut.wechat.moments.utils.Result;
import org.glut.wechat.moments.vo.MainMomentItemVO;
import org.glut.wechat.moments.vo.MainMomentWrapperVO;
import org.glut.wechat.moments.vo.UserMomentItemVO;
import org.glut.wechat.moments.vo.UserMomentWrapperVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 朋友圈控制器
 */
@RestController
@RequestMapping("/moment")
public class MomentsController {

    @Autowired
    private MomentsService momentsService;

    /**
     * 发布朋友圈
     *
     * @return 响应结果
     */
    @PostMapping("/publish")
    public Result publishMoments(@RequestBody MomentPublishDTO momentPublishDTO) {
        momentsService.publishMoments(momentPublishDTO);
        return Result.ok("发布成功");
    }

    @PutMapping("/update")
    public Result updateMoments(@RequestBody MomentUpdateDTO momentPublishDTO) {
        momentsService.updateMoments(momentPublishDTO);
        return Result.ok("修改成功");
    }

    /**
     * 分页查询公开的朋友圈
     *
     * @return 响应结果
     */
    @GetMapping("/public")
    public Result getPublicMoments(@RequestParam(defaultValue = "1") Integer pageNo,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(value = "userId") Integer userId) {
        Page<MainMomentItemVO> page = new Page(pageNo, pageSize);
        MainMomentWrapperVO result = momentsService.getPublicMoments(page, userId);
        return Result.ok("查询成功", result);
    }

    /**
     * 根据用户ID分页查询朋友圈
     *
     * @param pageNo 当前页码
     * @param pageSize    每页大小
     * @return 响应结果
     */
    @GetMapping("/userMoments/{lookUserId}/{currentUserId}")
    public Result getMomentsByUserId(@PathVariable Integer lookUserId,
                                     @PathVariable Integer currentUserId,
                                     @RequestParam(defaultValue = "1") Integer pageNo,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<UserMomentItemVO> page = new Page(pageNo, pageSize);
        UserMomentWrapperVO result = momentsService.getMomentsByUserId(page, lookUserId,currentUserId);
        return Result.ok("查询成功", result);
    }

    /**
     * 根据ID获取朋友圈详情
     *
     * @param id 朋友圈ID
     * @return 响应结果
     */
    @GetMapping("/getMomentsById/{id}/{userId}")
    public Result getMomentsById(@PathVariable Integer id,
                                 @PathVariable Integer userId) {
        MainMomentItemVO moment = momentsService.getMomentsById(id, userId);
        return Result.ok("查询成功", moment);
    }


    /**
     * 删除朋友圈
     *
     * @param id 朋友圈ID
     * @return 响应结果
     */
    @DeleteMapping("/deleteMoments/{id}")
    public Result deleteMoments(@PathVariable Integer id) {
        momentsService.deleteMoments(id);
        return Result.ok("删除成功");
    }
}
